<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container" v-if="!listLoading">
      <el-row :gutter="20">
        <el-col :span="2">
          <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit">添加员工</el-button>
        </el-col>
        <el-col :span="6">
          <el-input placeholder="请输入关键字" v-model="query.keyword">
            <i slot="suffix" class="el-input__icon el-icon-search" @click="loadTable"></i>
          </el-input>
        </el-col>
      </el-row>
    </div>
    <el-table v-loading="listLoading" :data="tableData" border style="width: 100%">
      <el-table-column prop="username" label="用户名" width="180"></el-table-column>
      <el-table-column prop="realname" label="真名" width="180"></el-table-column>
      <el-table-column prop="role_text" label="职位" width="180">
        <!--<template scope="scope">-->
          <!--{{roleName[scope.row.role]}}-->
        <!--</template>-->
      </el-table-column>
      <el-table-column prop="mobile" label="手机号码"></el-table-column>
      <!--<el-table-column prop="email" label="邮箱"></el-table-column>-->
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="primary" size="small" @click="editPassword(scope.row)">修改密码</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
      </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <template v-if="!listLoading">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :total="query.pagination.total"
          :page-sizes="[5, 10, 15, 20, 30]"
          :page-size="query.pagination.per_page"
          :current-page.sync="query.pagination.current_page"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </template>
    </div>
    <edit
      :editId="editId"
      :dialogVisible="dialogVisible"
      :title="title"
      @closeDialog="closeDialog">
    </edit>
    <password
      :editId="editId"
      :dialogVisible="editPasswordVisible"
      :title="title"
      @closeDialog="closeDialog">
    </password>
  </div>
</template>

<script>
import edit from './edit'
import password from './password'
import { GetTable, Delete } from '@/api/staff'

export default {
  components: {
    edit,
    password
  },
  data() {
    return {
      roleName: {
        'receptionist': '前台',
        'manager': '经理'
      },
      editId: null,
      visible: [],
      delVisible: false,
      dialogVisible: false,
      editPasswordVisible: false,
      title: null,
      listLoading: false,
      query: {
        pagination: {
          total: 0,
          page: 1,
          per_page: 10
        },
        keyword: ''
      },
      tableData: []
    }
  },
  created() {
    console.log('table created')
    this.loadTable()
  },
  methods: {
    loadTable() {
      this.listLoading = true
      GetTable(this.query.pagination.current_page, this.query.pagination.per_page, this.query.keyword).then((res) => {
        this.listLoading = false
        this.tableData = res.data
        this.query.pagination = res.meta.pagination
      })
    },
    handleSizeChange(val) {
      this.query.pagination.per_page = val
      this.loadTable()
    },
    handleCurrentChange(val) {
      this.query.pagination.current_page = val
      this.loadTable()
    },
    handleCreate() {
      console.log('action: create')
      this.title = '添加'
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.editId = row.uuid
      this.title = '编辑'
      this.dialogVisible = true
    },
    editPassword(row) {
      this.editId = row.uuid
      this.title = '编辑'
      this.editPasswordVisible = true
    },
    handleDelete(row) {
      console.log('action: delete; row:', row)
      this.$confirm('此操作将删除一条记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        Delete(row.uuid).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.loadTable()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    closeDialog(loadTable) {
      this.dialogVisible = false
      this.editPasswordVisible = false
      if (loadTable) this.loadTable()
    }
  }
}
</script>
